#include "mytableview.h"
#include <QHeaderView>
#include <QTimer>
#include <QMessageBox>

MyTableView::MyTableView(QWidget *parent) : QTableView(parent) {
    // // //表格
    standItemModel = new QStandardItemModel();
    setModel(standItemModel);                                            //挂载表格模型
    // 设置表格属性
    horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);              //表头信息显示居
    setEditTriggers(QAbstractItemView::SelectedClicked);                 //设置表格属性(如只读，不能编辑)
    setSelectionBehavior(QTableView::SelectRows);                        //整行选中
    resizeColumnToContents(0);
    QPalette p = palette();
    p.setColor(QPalette::Inactive, QPalette::Highlight, p.color(QPalette::Active, QPalette::Highlight));
    p.setColor(QPalette::Inactive, QPalette::HighlightedText, p.color(QPalette::Active, QPalette::HighlightedText));
    setPalette(p);
    setContextMenuPolicy(Qt::CustomContextMenu);
    // 委托
    delegate = new MyRowDelegate();
    setItemDelegate(delegate);
}

MyTableView::~MyTableView()
{
    if(standItemModel) standItemModel->deleteLater();
    if(delegate) delegate->deleteLater();
}

// 跳转到行
void MyTableView::scrollToRow(int rowTmp)
{
    scrollTo(standItemModel->index(rowTmp, 0));
    bool isSelect=selectionModel()->isSelected(standItemModel->index(rowTmp, 0));
    for (int row=0; row < standItemModel->rowCount(); row++){
        selectionModel()->select(standItemModel->index(rowTmp,0), isSelect?QItemSelectionModel::Deselect:QItemSelectionModel::Select);
    }
    QTimer::singleShot(400, this, [=](){
        for (int row=0; row < standItemModel->rowCount(); row++){
            selectionModel()->select(standItemModel->index(rowTmp,0), isSelect?QItemSelectionModel::Select:QItemSelectionModel::Deselect);
        }
    });
}

// 获取表头数据
int MyTableView::getHeaders(QStringList &d_tmp){
    if(!d_tmp.empty()) d_tmp.clear();
    for(int i=0;i<standItemModel->columnCount();i++){
        d_tmp.push_back(standItemModel->headerData(i, Qt::Horizontal).toString());
    }
    return d_tmp.size();
}

// 获取表数据(存入数据容器，是否保存打印日志，是否获取不选中行的数据，是否获取隐藏行的数据)
int MyTableView::getDatas(QVector<QStringList> &v_data, bool isSaveLog, bool isGetNoneSelected, bool isGetHiddened){
    if(!v_data.empty()) v_data.clear();
    bool have_green=false;
    QSet<int> green_set_tmp;
    for (int r=0; r<standItemModel->rowCount(); r++) {
        bool isSelect=selectionModel()->isSelected(standItemModel->index(r,0));
        if((!isGetNoneSelected&&!isSelect)||(!isGetHiddened&&isRowHidden(r))) continue;
        QStringList v_str;
        if(!have_green&&delegate->isRowGreen(r)){
            have_green=true;
        }
        for (int c=0; c<standItemModel->columnCount(); c++) {
            QVariant data = standItemModel->data(standItemModel->index(r, c), Qt::DisplayRole);
            if(data.isValid()){
                v_str.push_back(data.toString());
                if(isSaveLog) green_set_tmp.insert(r);
            }else{
                v_str.push_back("");
            }
        }
        if(!v_str.empty())
            v_data.push_back(v_str);
    }
    if(isSaveLog){
        if(have_green){
            if(QMessageBox::No==QMessageBox::question(this, "警告", "选中行中存在已打印的数据，是否继续？", QMessageBox::Yes|QMessageBox::No, QMessageBox::No))
                return -1;
        }
        delegate->addGreenRows(green_set_tmp); // 已打印，赋绿色
    }
    return v_data.size();
}

// 设置表数据
void MyTableView::setDatas(QVector<QStringList> &v_data){
    if(v_data.empty()) return;
    standItemModel->clear();
    // 设置表头
    standItemModel->setHorizontalHeaderLabels(v_data[0]);
    v_data.removeAt(0);
    if(v_data.empty()) return;
    // 其他数据
    setUpdatesEnabled(0);
    standItemModel->setRowCount(v_data.size());
    standItemModel->setColumnCount(v_data[0].size());
    for (int r = 0; r < v_data.size(); ++r) {
        for (int c = 0; c < v_data[r].size(); ++c) {
            QModelIndex index = standItemModel->index(r, c);
            standItemModel->setData(index, v_data[r][c], Qt::DisplayRole);
            standItemModel->setData(index, int(Qt::AlignLeft | Qt::AlignVCenter),Qt::TextAlignmentRole);
        }
    }
    resizeColumnsToContents();
    setUpdatesEnabled(1);
}
